package com.itwill.ecom.board;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("boardService")
public class BoardServiceImpl implements BoardService {
	@Autowired
	private BoardDao boardDao;
	
	public BoardServiceImpl() {
		System.out.println("#### BoardService()");
	}

	public BoardServiceImpl(BoardDao boardDao) {
		System.out.println("#### BoardService(BoardDao boardDao)");
		this.boardDao = boardDao;
	}
	
	public void setBoardDao(BoardDao boardDao) {
		System.out.println("#### BoardService : setBoardDao(BoardDao boardDao)");
		this.boardDao = boardDao;
	}

	private BoardDao getBoardDao() {
		System.out.println("#### BoardService : getBoardDao()");
		return this.boardDao;
	}
	
	@Override
	public boolean createBoard(BoardBeans boardBeans){
		boolean result = false;
		
		try{
			getBoardDao().create(boardBeans);
			result = true;
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		return result;
	}
	
	@Override
	public List<BoardBeans> getAllBoard(HashMap<String, String> pageMap){
		List<BoardBeans> resultList = new ArrayList<BoardBeans>();

		try{
			resultList = getBoardDao().readAll(pageMap);
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		return resultList;
	}
	
	@Override
	public List<BoardBeans> getBoardByType(HashMap<String, String> pageMap){
		List<BoardBeans> resultList = new ArrayList<BoardBeans>();

		try{
			resultList = getBoardDao().readByType(pageMap);
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		return resultList;
	}
	
	@Override
	public BoardBeans getBoardByNo(int boardno){
		BoardBeans resultBoard = null;
		
		try{
			resultBoard = getBoardDao().readByNo(boardno);
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		return resultBoard;
	}
	
	@Override
	public List<BoardBeans> getBoardByNickname(HashMap<String, String> pageMap){
		List<BoardBeans> resultList = new ArrayList<BoardBeans>();

		try{
			resultList = getBoardDao().readByNickname(pageMap);
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		return resultList;
	}

	@Override
	public List<BoardBeans> getBoardById(HashMap<String, String> pageMap){
		List<BoardBeans> resultList = new ArrayList<BoardBeans>();

		try{
			resultList = getBoardDao().readById(pageMap);
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		return resultList;
	}

	@Override
	public List<BoardBeans> getBoardByTitle(HashMap<String, String> pageMap){
		List<BoardBeans> resultList = new ArrayList<BoardBeans>();

		try{
			resultList = getBoardDao().readByTitle(pageMap);
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		return resultList;
	}

	@Override
	public List<BoardBeans> getBoardByContent(HashMap<String, String> pageMap){
		List<BoardBeans> resultList = new ArrayList<BoardBeans>();

		try{
			resultList = getBoardDao().readByContent(pageMap);
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		return resultList;
	}
	
	@Override
	public int getAllCount(){
		int count = 0;
		
		try{
			count = getBoardDao().readAllCount();
		}
		catch(Exception e){
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		
		return count;
	}
	
	@Override
	public int getCountByType(String type){
		int count = 0;
		
		try{
			count = getBoardDao().readCountByType(type);
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		return count;
	}
	
	@Override
	public int getMaxNo(String id){
		int count = 0;
		
		try{
			count = getBoardDao().readMaxNo(id);
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		return count;
	}

	@Override
	public boolean updateBoard(BoardBeans updateBoard){
		boolean result = false;
		
		try{
			getBoardDao().update(updateBoard);
			result = true;
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		return result;
	}
	
	@Override
	public boolean updateRecommend(int boardno){
		boolean result = false;
		
		try{
			getBoardDao().updateRecommend(boardno);
			result = true;
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		return result;
	}
	
	@Override
	public boolean updateShow(int boardno){
		boolean result = false;
		
		try{
			getBoardDao().updateShow(boardno);
			result = true;
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		return result;
	}

	@Override
	public boolean deleteBoard(int board_no){
		boolean result = false;
		
		try{
			getBoardDao().delete(board_no);
			result = true;
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		return result;
	}
}